use criterion::{black_box, criterion_group, criterion_main, Criterion};
use avl_tree::avl_tree::{AVLTree, AVLTreeCompareFunc};

fn compare_i32(left: &i32, right: &i32) -> std::cmp::Ordering {
    left.cmp(right)
}

fn avl_tree_insert_benchmark(c: &mut Criterion) {
    let mut tree = AVLTree::new(compare_i32);
    c.bench_function("avl_tree_insert", |b| {
        b.iter(|| {
            for i in 0..1000 {
                tree.insert(black_box(i), black_box(i));
            }
        })
    });
}

fn avl_tree_lookup_node_benchmark(c: &mut Criterion) {
    let mut tree = AVLTree::new(compare_i32);
    for i in 0..1000 {
        tree.insert(i, i);
    }
    c.bench_function("avl_tree_lookup_node", |b| {
        b.iter(|| {
            for i in 0..1000 {
                tree.lookup_node(black_box(&i));
            }
        })
    });
}

fn avl_tree_to_array_benchmark(c: &mut Criterion) {
    let mut tree = AVLTree::new(compare_i32);
    for i in 0..1000 {
        tree.insert(i, i);
    }
    c.bench_function("avl_tree_to_array", |b| {
        b.iter(|| {
            tree.to_array();
        })
    });
}

fn avl_tree_num_entries_benchmark(c: &mut Criterion) {
    let mut tree = AVLTree::new(compare_i32);
    for i in 0..1000 {
        tree.insert(i, i);
    }
    c.bench_function("avl_tree_num_entries", |b| {
        b.iter(|| {
            tree.num_entries();
        })
    });
}

criterion_group!(
    benches,
    avl_tree_insert_benchmark,
    avl_tree_lookup_node_benchmark,
    avl_tree_to_array_benchmark,
    avl_tree_num_entries_benchmark
);
criterion_main!(benches);